<?php

namespace app\controllers\shop;

use app\models\shop\GoodsModel;
use app\models\shop\MerchantCategoryGroupModel;
use app\models\shop\MerchantCategoryModel;
use app\models\shop\ShopGoodsModel;
use app\models\shop\ShopSpecialModel;
use yii;
use yii\web\ShopController;

class SpecialController extends ShopController {

    public $enableCsrfValidation = false; //禁用CSRF令牌验证，可以在基类中设置

    public function behaviors() {
        return [
            'token' => [
                'class' => 'yii\filters\ShopFilter', //调用过滤器
//                'only' => ['single'],//指定控制器应用到哪些动作
                'except' => ['list','one'],//指定控制器不应用到哪些动作
            ]
        ];
    }

    /**
     * 专题列表
     */
    public function actionList() {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $model = new ShopSpecialModel();
            $time = time();
            $params['or'] = ['and',['>','end_time', $time]];
            $params['status'] = 1;
            $array = $model->do_select($params);
            if ($array['status'] == 200) {
                foreach ($array['data'] as $k=>$v){
                    if ($v['start_time'] > $time) {
                        $array['data'][$k]['activity_status'] = '未开始';
                    } elseif ($v['end_time'] < $time) {
                        $array['data'][$k]['activity_status'] = '已结束';
                    } else {
                        $array['data'][$k]['activity_status'] = '进行中';
                    }
                    $array['data'][$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                    $array['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
                }
            }
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    /**
     * 专题详情
     */
    public function actionOne($id) {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $model = new ShopSpecialModel();
            $where['id'] = $id;
            $array = $model->do_one($where);
            if ($array['status'] == 200) {
                $array['data']['start_time'] = date('Y-m-d H:i:s',$array['data']['start_time']);
                $array['data']['end_time'] = date('Y-m-d H:i:s',$array['data']['end_time']);
                $array['data']['pic_urls'] = explode(',',$array['data']['pic_urls']);
                if (!empty($array['data']['goods_ids'])) {
                    $goodsModel = new ShopGoodsModel();
                    $goodsIds = json_decode($array['data']['goods_ids'],true);
                    $goodsWhere['field'] = "id,name,pic_urls,price,line_price,simple_info,( select IFNULL( sum(number),0 ) from `shop_order` where `shop_order`.goods_id = `shop_goods`.`id` ) as sold ";
                    $goodsWhere['in'] = ['id',$goodsIds];
                    $goodsWhere['limit'] = false;
                    $goodsInfo = $goodsModel->do_select($goodsWhere);
                    if ($goodsInfo['status'] != 200){
                        return result(500, "商品信息有误");
                    }
                    foreach ($goodsInfo['data'] as $k=>$v){
                        $pic = explode(',',$v['pic_urls']);
                        $goodsInfo['data'][$k]['pic_urls'] = $pic[0];
                    }
                    $array['data']['goods'] = $goodsInfo['data'];
                }
            }
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

}
